package gold.digger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC65 {
    public long startExecuteTime = System.currentTimeMillis();

    /*
         * @param 此题目参考了别人代码
         * 这是因为问题情况较为复杂
         * 而这又是面向测试用例编程的一道题目
         * 可以借鉴思想，多多学习
         * @return:
         */
    class Solution {
        public boolean isNumber(String s) {
            if (s == null)
                return false;
            int start = 0, end = s.length() - 1;
            while (start <= end && s.charAt(start) == ' ') {
                start++;
            }
            while (end >= start && s.charAt(end) == ' ') {
                end--;
            }
            if (start > end)
                return false;
            boolean hasNum = false;
            boolean hasE = false;
            boolean hasDot = false;
            if (s.charAt(start) == '+' || s.charAt(start) == '-') {
                start++;
            }
            // first judge +/-
            while (start <= end) {

                if (s.charAt(start) >= '0' && s.charAt(start) <= '9') {
                    // is number
                    hasNum = true;
                } else if (s.charAt(start) == 'e') {
                    if (hasE || !hasNum)
                        return false;
                    hasE = true;
                    hasNum = false;

                } else if (s.charAt(start) == '.') {
                    if (hasDot || hasE)
                        return false;
                    hasDot = true;
                } else if (s.charAt(start) == '+' || s.charAt(start) == '-') {
                    if (s.charAt(start - 1) != 'e')
                        return false;
                } else {
                    return false;
                }

                start++;
            }

            return hasNum;

        }
    }

    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC65 an = new LC65();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
